package com.letwal.cmcvnd.components.dialog.selectAsset.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.coin.RevDataGetCoins
import com.letwal.cmcvnd.extend.isLink
import com.letwal.cmcvnd.extend.toFinanceString

class SelectAssetAdapter(private val dataList: List<RevDataGetCoins>) :
    RecyclerView.Adapter<SelectAssetAdapter.ViewHolder>() {

    //点击事件
    private var mItemClickListener: (RevDataGetCoins) -> Unit = {}

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var rlAdapterSelectAssetContent: RelativeLayout =
            view.findViewById(R.id.rlAdapterSelectAssetContent)
        var imageAdapterSelectAsset: ImageView =
            view.findViewById(R.id.imageAdapterSelectAsset)
        var textAdapterSelectAssetName: TextView =
            view.findViewById(R.id.textAdapterSelectAssetName)
        var textAdapterSelectAssetAmount: TextView =
            view.findViewById(R.id.textAdapterSelectAssetAmount)
        var textAdapterSelectAssetAmountRef: TextView =
            view.findViewById(R.id.textAdapterSelectAssetAmountRef)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_select_asset, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevDataGetCoins = dataList[position]
        holder.textAdapterSelectAssetName.text = data.coinName
        holder.textAdapterSelectAssetAmount.text = data.balance?.toFinanceString()

        holder.rlAdapterSelectAssetContent.setOnClickListener {
            mItemClickListener.invoke(data)
        }

        if (data.logo.toString().isLink()) {
            Glide.with(holder.imageAdapterSelectAsset.context).load(data.logo)
                .into(holder.imageAdapterSelectAsset)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (RevDataGetCoins) -> Unit) {
        mItemClickListener = listener
    }
}